package com.fast.alden.portal.service.impl;

import com.fast.alden.common.entity.dto.ChangePasswordParam;
import com.fast.alden.common.exception.BizException;
import com.fast.alden.common.exception.BizExceptionEnum;
import com.fast.alden.data.model.PortalUser;
import com.fast.alden.data.repo.PortalUserRepository;
import com.fast.alden.portal.entity.dto.PortalUserProfile;
import com.fast.alden.portal.service.AuthPortalService;
import com.fast.alden.portal.service.PersonalCenterPortalService;
import com.fast.alden.portal.service.PortalUserPortalService;
import com.fast.alden.portal.util.PortalSecurityUtils;
import jakarta.annotation.Resource;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class PersonalCenterPortalServiceImpl implements PersonalCenterPortalService {
    @Resource
    private PortalUserRepository portalUserRepository;
    @Resource
    private AuthPortalService authPortalService;
    @Resource
    private PortalUserPortalService userPortalService;
    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public PortalUserProfile retrieve() {
        PortalUser current = PortalSecurityUtils.getCurrentUser();
        if (current == null) {
            throw new BizException(BizExceptionEnum.USER_DOSE_NOT_LOGGED_IN);
        }
        PortalUser user = portalUserRepository.findById(current.getId()).orElse(null);

        PortalUserProfile userProfile = new PortalUserProfile();
        userProfile.setUser(user);

        return userProfile;
    }

    @Override
    public void update(PortalUser user) {
        PortalUser current = PortalSecurityUtils.getCurrentUser();
        if (current == null) {
            throw new BizException(BizExceptionEnum.USER_DOSE_NOT_LOGGED_IN);
        }
        userPortalService.update(user);
    }

    @Override
    public void changePassword(ChangePasswordParam param) {
        authPortalService.checkVerifyCode(param.getVerifyCodeKey(), param.getVerifyCode());

        PortalUser current = PortalSecurityUtils.getCurrentUser();
        if (current == null) {
            throw new BizException(BizExceptionEnum.USER_DOSE_NOT_LOGGED_IN);
        }
        PortalUser user = userPortalService.retrieveWithException(current.getId());
        if (!passwordEncoder.matches(param.getOldPassword(), user.getPassword())) {
            throw new BizException(BizExceptionEnum.USER_OLD_PASSWORD_IS_WRONG);
        }
        if (param.getOldPassword().equals(param.getNewPassword())) {
            throw new BizException(BizExceptionEnum.NEW_PASSWORD_IS_SAME_AS_OLD_PASSWORD);
        }
        userPortalService.updatePassword(current.getId(), param.getNewPassword());

        authPortalService.logout();
    }

    @Override
    public void updateAvatar(MultipartFile file) throws IOException {
        PortalUser current = PortalSecurityUtils.getCurrentUser();
        if (current == null) {
            throw new BizException(BizExceptionEnum.USER_DOSE_NOT_LOGGED_IN);
        }
        userPortalService.updateAvatar(current.getId(), file);
    }

    @Override
    public void removeAvatar() {
        PortalUser current = PortalSecurityUtils.getCurrentUser();
        if (current == null) {
            throw new BizException(BizExceptionEnum.USER_DOSE_NOT_LOGGED_IN);
        }
        userPortalService.removeAvatar(current.getId());
    }
}
